<?php
/*基本控制器*/
abstract class Controller{
	private $view_data;
	protected $default_action;
	
	function __construct(){
		$this ->view_data = array();
	}
	
	protected function assign( $key, $value ){
		$this ->view_data[$key] = $value;
	}
	
	protected function diplay( $tpl_file ){
		include($tpl_file);
	}	
	
	function __call(){
		if ($GLOBALS['request']['action'] == 'default') $this ->jump($default_action);
		else $this ->NotFoundAction();
	}
	
	public function jump(){
		if (func_num_args() = 1){
			header( 
				   'Location: '.make_url( 
										 $GLOBALS['request']['module'],
										 func_get_arg(0)
										 )
				   );
		}else if (func_num_args() > 1){
			header( 
				   'Location: '.make_url( 
										 func_get_arg(0),
										 func_get_arg(1)
										 )
				   );
		}
	}
	
	public function make_url( $module = NULL, $action = NULL, $parameters = NULL  ){
		
		$url = '/';
		
		if ( REWRITE ){
			if ( $module != NULL ) $url .= $module.'/';
			if ( $action != NULL ) $url .= $action.'/';
			if ( $parameters != NULL ) {
				if (is_array($parameters)){
					foreach ($parameters as $v) $url .= $v.'/';
				}else{
					$url .= $parameters.'/';
				}
			}
		}else{
			
			$url .= 'index.php';
			
			if ( $module != NULL ) $url .= '?module='.$module;
			if ( $action != NULL ) $url .= '&action='.$action;
			if ( $parameters != NULL ) {
				
				$url .= '&parameters=';
				
				if (is_array($parameters)){
					foreach ($parameters as $v) $url .= $v.'/';
				}else{
					$url .= $parameters;
				}
			}
		}
	}
	
	public function NotFoundAction(){
		$this ->display(TPL_DIR.'/'.TPL_NAME.'/notfound.tpl.php');
	}
}

?>